package rbac.models;

import java.sql.Date;

import utils.DateUtil;

import com.et.ar.ActiveRecordBase;
import com.et.ar.annotations.BelongsTo;
import com.et.ar.annotations.Column;
import com.et.ar.annotations.Id;
import com.et.ar.annotations.Table;
import com.et.ar.annotations.Unique;

import models.Userlogin;
import models.Zjlist;
import java.sql.*;

@Table(name = "users")
public class User extends ActiveRecordBase {
	@Id
	private Integer id;
	@Column
	private Integer departmentid;
	@Column
	private Integer dutyid;
	@Column
	@Unique(message = "用户名称不能重复")
	private String name;
	@Column
	private String sex;
	@Column
	private java.sql.Date birthday;
	@Column
	private String address;
	@Column
	@Unique(message = "登录名不能重复")
	private String login;
	@Column
	private String password;
	@Column
	private String monumber;
	@Column
	private String shortnumber;
	@Column
	private String homenumber;
	@Column
	private String email;
	@Column
	private java.sql.Date intime;
	@Column
	private java.sql.Date outtime;
	@Column
	private String optime1;
	@Column
	private String optime2;
	@Column
	private String loginurl;
	@Column
	private String activated;
	@Column
	private String remark;
	@Column
	private String roleids;

	@BelongsTo(foreignKey = "departmentid")
	private Zjlist department;

	@BelongsTo(foreignKey = "dutyid")
	private Duty duty;

	public long getTimeout() throws Exception{
		Timestamp at = Userlogin.getUserActiveTime(login);
		if (at == null)
			return 3600000;
		else
		    return System.currentTimeMillis() - at.getTime();		
	}
	
	public String getRoles() throws Exception {
		if (roleids == null)
			return null;
		String s = "";
		String[] ids = roleids.split(",");
		for (String id : ids) {
			Role role = Role.find(Role.class, id);
			if (role != null) {
				s += role.getName() + ",";
			} else {
				s += id + ",";
			}
		}
		if (s.equals("")) {
			return "";
		} else {
			return s.substring(0, s.length() - 1);
		}
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getDepartmentid() {
		return departmentid;
	}

	public void setDepartmentid(Integer departmentid) {
		this.departmentid = departmentid;
	}

	public Integer getDutyid() {
		return dutyid;
	}

	public void setDutyid(Integer dutyid) {
		this.dutyid = dutyid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public void setBirthday(java.sql.Date birthday) {
		this.birthday = birthday;
	}

	public java.sql.Date getBirthday() {
		return birthday;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getAddress() {
		return address;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMonumber() {
		return monumber;
	}

	public void setMonumber(String monumber) {
		this.monumber = monumber;
	}

	public Zjlist getDepartment() {
		return department;
	}

	public void setDepartment(Zjlist department) {
		this.department = department;
	}

	public Duty getDuty() {
		return duty;
	}

	public void setDuty(Duty duty) {
		this.duty = duty;
	}

	public String getRoleids() {
		return roleids;
	}

	public void setRoleids(String roleids) {
		this.roleids = roleids;
	}

	public String getShortnumber() {
		return shortnumber;
	}

	public void setShortnumber(String shortnumber) {
		this.shortnumber = shortnumber;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}

	public java.sql.Date getIntime() {
		return intime;
	}

	public void setIntime(java.sql.Date intime) {
		this.intime = intime;
	}

	public java.sql.Date getOuttime() {
		return outtime;
	}

	public void setOuttime(java.sql.Date outtime) {
		this.outtime = outtime;
	}

	public String getOptime1() {
		return optime1;
	}

	public void setOptime1(String optime1) {
		this.optime1 = optime1;
	}
	
	public String getOptime2() {
		return optime2;
	}

	public void setOptime2(String optime2) {
		this.optime2 = optime2;
	}
	
	public String getHomenumber() {
		return homenumber;
	}

	public void setHomeNumber(String homenumber) {
		this.homenumber = homenumber;
	}

	public String getLoginurl() {
		return loginurl;
	}

	public void setLoginurl(String loginurl) {
		this.loginurl = loginurl;
	}

	public void setActivated(String activated) {
		this.activated = activated;
	}

	public String getActivated() {
		return activated;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getRemark() {
		return remark;
	}
}
